﻿using System;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

using System.Windows.Forms;

namespace CIS470MetricsTracking
{
	// You do not need to create a instance of this class.
	// The class is already instanciated in the static ControllerManager class.
	// To access just use ControllerManager.data_controller.<method you need>
	public class DataController
	{
		private OleDbConnectionStringBuilder conn_str;

		public DataController()
		{
			conn_str = new OleDbConnectionStringBuilder();
			conn_str.Provider = "Microsoft.ACE.OLEDB.12.0";
			conn_str.DataSource = "Database\\MT_Database.accdb";
		}

		// Returns true on success and false on failure.  ALL OTHER QUERIES
		public bool executeQuery(string query)
		{
			OleDbConnection conn = new OleDbConnection(conn_str.ConnectionString);

			try {
				conn.Open();
				if (!query.StartsWith("select")) {
					OleDbCommand cmd = new OleDbCommand(query, conn);
					cmd.ExecuteNonQuery();
				} else {
					return false;
				}
				return true;
			} catch (Exception e) {
				return false;
			} finally {
				conn.Close();
			}
		}

		// Return Null if the query fails for any reason.  MUST BE A SELECT QUERY
		public DataSet executeSelectQuery(string query)
		{
			OleDbConnection conn = new OleDbConnection(conn_str.ConnectionString);
			DataSet ds = new DataSet();

			try {
				conn.Open();
				if (query.StartsWith("select")) {
					OleDbDataAdapter adapt = new OleDbDataAdapter(query, conn);
					adapt.Fill(ds);
				} else {
					return null;
				}
				return ds;
			} catch (Exception e) {
				return null;
			} finally {
				conn.Close();
			}
		}

		public void loadExcelFile(string file_path)
		{

		}
	}
}
